//
//  HomeButtonView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/27.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

enum HomeButtonViewType: Int {
    case registration
    case advisory
    case cloud
    case team
    case myopia
    case nurse
    
    var title: String {
        switch self {
        case .registration: return "门诊预约"
        case .advisory: return "在线问诊"
        case .cloud: return "云门诊"
        case .team: return "团队问诊"
        case .nurse: return "护理预约"
        case .myopia: return ""
        }
    }
    
    var subTitle: String {
        switch self {
        case .registration: return ""
        case .advisory: return ""
        case .cloud: return ""
        case .team: return ""
        case .nurse: return ""
        case .myopia: return ""
        }
    }
    
    var image: UIImage? {
        switch self {
        case .registration: return UIImage(named: "home_registration_btn")
        case .advisory: return UIImage(named: "home_advisory_btn")
        case .cloud: return UIImage(named: "home_cloud_btn")
        case .team: return UIImage(named: "home_team_btn")
        case .nurse: return UIImage(named: "home_nurse_btn")
        case .myopia: return nil
        }
    }
}

class HomeButtonView: UIView {
    
    var tappedClosure: ((_ type: HomeButtonViewType) -> Void)?

    @IBOutlet private weak var imageView: UIImageView!
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var subTitleLabel: UILabel!
    
    private var viewType: HomeButtonViewType = .advisory
    
    func configure(with type: HomeButtonViewType) {
        viewType = type
        imageView.image = type.image
        titleLabel.text = type.title
        subTitleLabel.text = type.subTitle
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped(_:)))
        addGestureRecognizer(tap)
    }
    
    @objc private func viewTapped(_ tap: UITapGestureRecognizer) {
        tappedClosure?(viewType)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        backgroundColor = .mainLight
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        UIView.animate(withDuration: 0.15) {
            self.backgroundColor = .white
        }
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        UIView.animate(withDuration: 0.15) {
            self.backgroundColor = .white
        }
    }
}
